package com.relytosoft.mizar.media.display.services.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.relytosoft.dubhe.common.core.utils.TreeBuildUtils;
import com.relytosoft.dubhe.common.satoken.entity.CurrentUser;
import com.relytosoft.mizar.media.display.api.entity.Rotagroup;
import com.relytosoft.mizar.media.display.api.entity.RotagroupChannel;
import com.relytosoft.mizar.media.display.api.pojo.vo.RotagroupVo;
import com.relytosoft.mizar.media.display.services.mapper.RotagroupMapper;
import com.relytosoft.mizar.media.display.services.service.RotagroupChannelService;
import com.relytosoft.mizar.media.display.services.service.RotagroupService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * RotagroupServiceImpl
 *
 * @Author SunLi
 * @Date: 2022/4/11 18:48
 * @Version 1.0
 */
@Service
public class RotagroupServiceImpl  extends ServiceImpl<RotagroupMapper, Rotagroup> implements RotagroupService {

    @Resource
    private RotagroupChannelService rotagroupChannelService;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean insertRotagroup(Rotagroup rotagroup,List<RotagroupChannel> rotagroupChannelList) {
        boolean isSucc=this.save(rotagroup);
        if(isSucc){
            rotagroupChannelList.forEach(item->{
                item.setRotaGroupId(rotagroup.getId());
            });
            if (CollUtil.isNotEmpty(rotagroupChannelList)) {
                isSucc = rotagroupChannelService.saveBatch(rotagroupChannelList);
            }
        }
        return isSucc;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateRotagroup(Rotagroup rotagroup,List<RotagroupChannel> rotagroupChannelList) {
        boolean isSucc=this.updateById(rotagroup);
        if(isSucc){
            LambdaQueryWrapper<RotagroupChannel> wrapper=Wrappers.lambdaQuery();
            wrapper.eq(RotagroupChannel::getRotaGroupId,rotagroup.getId());
            rotagroupChannelService.remove(wrapper);
            rotagroupChannelList.forEach(item->{
                item.setRotaGroupId(rotagroup.getId());
            });
            if (CollUtil.isNotEmpty(rotagroupChannelList)) {
                isSucc = rotagroupChannelService.saveBatch(rotagroupChannelList);
            }
        }
        return isSucc;
    }

    @Override
    public int updateRotagroupStatus(int id,int status){
        return baseMapper.update(null,new LambdaUpdateWrapper<Rotagroup>().set(Rotagroup::getStatus,status).set(Rotagroup::getUpdatedId, CurrentUser.getCurrentUserId()).set(Rotagroup::getUpdatedBy, CurrentUser.getCurrentUserFullName()).set(Rotagroup::getUpdatedTime, LocalDateTime.now()).eq(Rotagroup::getId,id));
    }

    @Override
    public boolean checkRotagroupNameUnique(Rotagroup rotagroup) {
        int id = ObjectUtil.isNull(rotagroup.getId()) ? -1 : rotagroup.getId();
        long count = count(new LambdaQueryWrapper<Rotagroup>()
                .eq(Rotagroup::getGroupName, rotagroup.getGroupName())
                .eq(Rotagroup::getStatus, 1)
                .eq(Rotagroup::getCreatedId, CurrentUser.getCurrentUserId())
                .ne(Rotagroup::getId, id));
        return count <= 0;
    }

    @Override
    public List<RotagroupVo> selectRotagroupList(String name) {
        return baseMapper.selectRotagroupList(CurrentUser.getCurrentUserId(),name,"HK");
    }

    @Override
    public List<Tree<Integer>> buildRotagroupListTree(List<RotagroupVo> rotagroupVoList) {
        if (CollUtil.isEmpty(rotagroupVoList)) {
            return CollUtil.newArrayList();
        }
        return TreeBuildUtils.build(rotagroupVoList, (v, tree) -> {
            tree.setId(v.getId());
            tree.setParentId(v.getParentId());
            tree.setName(v.getGroupName());
            tree.setWeight(v.getSort());
            tree.put("rotaInterval", v.getRotaInterval());
            tree.put("layout", v.getLayout());
            tree.put("channelId", v.getChannelId());
            tree.put("channelMainRtspUrl", v.getChannelMainRtspUrl());
            tree.put("channelSubRtspUrl", v.getChannelSubRtspUrl());
            tree.put("imgUrl", v.getImgUrl());
            tree.put("platformChannelCode", v.getPlatformChannelCode());
        });
    }
}
